#include "widget.h"
#include "ui_widget.h"
#include <QNetworkReply>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    manager = new QNetworkAccessManager(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    // 1. 获取到输⼊框中的URL, 构造QUrl 对象

    QUrl url(ui->lineEdit->text());
    // 2. 构造HTTP 请求对象

    QNetworkRequest request(url);
    // 3. 发送GET 请求

    QNetworkReply* response = manager->get(request);
    // 4. 通过信号槽来处理响应

    connect(response, &QNetworkReply::finished, this, [=]() {
    if (response->error() == QNetworkReply::NoError)
    {
        // 响应正确
        QString html(response->readAll());
        ui->plainTextEdit->setPlainText(html);
    }
    else
    {
        // 响应出错
        ui->plainTextEdit->setPlainText(response->errorString());
    }
    response->deleteLater();
    });
}
